package xml;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class ParseXml {
	public Document document;
	public void loadItem(){
		// 创建解析器工厂对象
		DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
		try {
			//通过解析器工厂对象创建解析器对象
			DocumentBuilder db = fac.newDocumentBuilder();
			document = db.parse("src/xml/showList.xml");
			NodeList movies = document.getElementsByTagName("Movie");
			for(int i = 0; i < movies.getLength(); i++){
				Node movie = movies.item(i);
				Element eleMovie = (Element)movie;
				NodeList nodeNames = eleMovie.getChildNodes();
				for(int j = 0; j < nodeNames.getLength(); j++){
					Node node = nodeNames.item(j);
					if(node.getChildNodes() != null){
						NodeList nodes = node.getChildNodes();
						for(int k = 0; k < nodes.getLength(); k++){
							Node childNode = nodes.item(k);
							Element eleNode = (Element)childNode;
							eleNode.getTextContent();
						}
					}
					Element ele = (Element)node;
					String txt = ele.getTextContent();
					System.out.println(txt);
				}
				
			}
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public static void main(String[] args) {
		ParseXml parseXml = new ParseXml();
		parseXml.loadItem();
	}
		
}
